package jhsys.mc.device;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import jhsys.mc.smarthome.data.DeviceConfig;
import jhsys.mc.smarthome.data.DeviceUtil;




public class GFSP {
	private String name;
	private String ico;
	private int type;
	private int id;
	private int lcid;
	private int fjid;
	private int rfid;
	private short value;
	private short state;
	private short x;
	private short y;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getIco() {
		return ico;
	}
	public void setIco(String ico) {
		this.ico = ico;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getLcid() {
		return lcid;
	}
	public void setLcid(int lcid) {
		this.lcid = lcid;
	}
	public int getFjid() {
		return fjid;
	}
	public void setFjid(int fjid) {
		this.fjid = fjid;
	}
	public int getRfid() {
		return rfid;
	}
	public void setRfid(int rfid) {
		this.rfid = rfid;
	}
	public short getValue() {
		return value;
	}
	public void setValue(short value) {
		this.value = value;
	}
	public short getState() {
		return state;
	}
	public void setState(short state) {
		this.state = state;
	}
	public short getX() {
		return x;
	}
	public void setX(short x) {
		this.x = x;
	}
	public short getY() {
		return y;
	}
	public void setY(short y) {
		this.y = y;
	}


	public static List<GFSP> read(){
		List<GFSP> result=new ArrayList<GFSP>();
		String filename=DeviceConfig.filePath+DeviceConfig.GFSP_filename;
		RandomAccessFile randomA = null;
		try {
			randomA = new RandomAccessFile(filename, "rw");
			byte[] lclb=new byte[68];
			while(randomA.read(lclb)==68){
				GFSP temp=new GFSP();
				temp.setName(DeviceUtil.byteToString(lclb, 0, 20));
				temp.setIco(DeviceUtil.byteToString(lclb, 20, 20));
				temp.setType(DeviceUtil.byte4ToInt(lclb, 40));
                temp.setId(DeviceUtil.byte4ToInt(lclb, 44));
                temp.setLcid(DeviceUtil.byte4ToInt(lclb, 48));
                temp.setFjid(DeviceUtil.byte4ToInt(lclb, 52));
                temp.setRfid(DeviceUtil.byte4ToInt(lclb, 56));
                temp.setValue(DeviceUtil.byte2ToShort(lclb, 60));
                temp.setState(DeviceUtil.byte2ToShort(lclb, 62));
                temp.setX(DeviceUtil.byte2ToShort(lclb, 64));
                temp.setY(DeviceUtil.byte2ToShort(lclb, 66));
				result.add(temp);
				lclb=new byte[68];
			}
			randomA.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if (null != randomA) {
				try {
					randomA.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
		
	}
	
	
	

	
	
	public static void write(List<GFSP> list){
		RandomAccessFile randomA = null;
		try {
			File file = new File(DeviceConfig.filePath
					+ DeviceConfig.GFSP_filename);
			if (file.exists()) {
				file.delete();
				file.createNewFile();
			}
			randomA = new RandomAccessFile(file, "rw");
			byte[] fjsp;
			for (GFSP item : list) {
				fjsp = new byte[68];
				System.arraycopy(DeviceUtil.stringTobyte(item.getName()), 0, fjsp, 0, 20);
				System.arraycopy(DeviceUtil.stringTobyte(item.getIco()), 0, fjsp, 20, 20);
				System.arraycopy(DeviceUtil.unsignedbyteTobyte(item.getType()), 0, fjsp, 40, 4);
				System.arraycopy(DeviceUtil.intTobytes(item.getId()), 0, fjsp, 44, 4);
				System.arraycopy(DeviceUtil.intTobytes(item.getLcid()), 0, fjsp, 48, 4);
				System.arraycopy(DeviceUtil.intTobytes(item.getFjid()), 0, fjsp, 52, 4);
				System.arraycopy(DeviceUtil.intTobytes(item.getRfid()), 0, fjsp, 56, 4);

				System.arraycopy(DeviceUtil.shortTobytes(item.getValue()), 0, fjsp, 60, 2);
				System.arraycopy(DeviceUtil.shortTobytes(item.getState()), 0, fjsp, 62, 2);
				System.arraycopy(DeviceUtil.shortTobytes(item.getX()), 0, fjsp, 64, 2);
				System.arraycopy(DeviceUtil.shortTobytes(item.getY()), 0, fjsp, 66, 2);
				randomA.write(fjsp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != randomA) {
				try {
					randomA.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}


	
	
	public String toString() {
		return "GFSP [fjid=" + Integer.toHexString(fjid) + ", ico=" + ico + ", id=" + Integer.toHexString(id) + ", lcid="
				+ Integer.toHexString(lcid) + ", name=" + name + ", rfid=" + Integer.toHexString(rfid) + ", state="
				+ Integer.toHexString(state) + ", type=" + Integer.toHexString(type) + ", value=" + Integer.toHexString(value) + ", x=" + Integer.toHexString(x)
				+ ", y=" + Integer.toHexString(y) + "]";
	}
	
	
}
